trigger SyncCreditCheckInfo on Order__c (before insert, before update) {
    Set<id> OptyIds = new Set<id>();
    order__c olOrder = new order__c();
    List<Opportunity> Opty = new List<Opportunity>();
    if(trigger.isUpdate){
    for(Order__c fulfillment : [select id,campaign_end_date__c,renewal_days__c,Renewal_Date__c,Opportunity__r.Ordered_Amount__c,Fulfillment_Status__c, Ordered_Product_Amount__c,Opportunity__c, opportunity__r.Credit_Check_Info_Secured__c, Credit_Check_Info_Secured__c from Order__c where id in :trigger.newMap.Keyset()]){
       if(fulfillment.Credit_Check_Info_Secured__c == 'Yes' && fulfillment.opportunity__r.Credit_Check_Info_Secured__c != 'Yes')
           OptyIds.add(fulfillment.Opportunity__c);
 }
           
    for(order__c O :trigger.new){
    //done thru coding coz one workflow update cannot fire another workflow task
        olOrder = trigger.oldMap.get(O.id);
       if(O.campaign_end_date__c != olOrder.campaign_end_date__c || O.renewal_days__c != olOrder.renewal_days__c ){
           double rdays = O.renewal_days__c;
           O.Renewal_Date__c = O.campaign_end_date__c - rdays.intValue();
       }   
        if(O.Fulfillment_Status__c != olOrder.Fulfillment_Status__c ){
            if(olOrder.Fulfillment_Status__c == 'Fulfillment Denied'){
                  O.Ordered_Product_Amount__c = O.Contracted_Product_Amount__c;
             }
            else if(O.Fulfillment_Status__c== 'Fulfillment Denied'){
                 O.Ordered_Product_Amount__c = 0.00;
             }
        }    
    }
           
    if(OptyIds.size()>0){       
    for(Opportunity O :[select Credit_Check_Info_Secured__c from Opportunity  where id in:OptyIds]){
        O.Credit_Check_Info_Secured__c = 'Yes';
        Opty.add(O);
    }}
    try{
        if(Opty.Size()>0)Update Opty;
        }
    Catch(DmlException e){
        system.debug('SyncCreditCheckInfo Trigger :Cannot Update Credit Check on Opty' + e);
     }}
     
     if(trigger.isInsert){
          for(order__c Odr :trigger.new){
           double rnwdays = Odr.renewal_days__c;
           Odr.Renewal_Date__c = Odr.campaign_end_date__c - rnwdays.intValue();
          }   
     }

}